package com.dap.cms.governance.controller;

import com.dap.dao.model.QueryResult;
import com.dap.cms.governance.po.CmsComponentPo;
import com.dap.cms.governance.service.CmsComponentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_component表对应的控制层类
 * @author: Fesine
 * @createTime:2018/08/21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/21
 */
@RestController
@RequestMapping("/v1")
public class CmsComponentController {
    private static final Logger logger=LoggerFactory.getLogger(CmsComponentController.class);
    @Autowired
    private CmsComponentService cmsComponentService;

    /**
     * 新增CmsComponentPo
     * @param cmsComponentPo
     * @return result
     */
    @PostMapping("/cmsComponent")
    public Result addCmsComponent(CmsComponentPo cmsComponentPo){
        try{
            cmsComponentService.add(cmsComponentPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("新增失败",e);
            return ResultUtils.error(500,"新增失败");
        }

    }

    /**
     * 更新CmsComponentPo
     * @param cmsComponentPo
     * @return result
     */
    @PutMapping("/cmsComponent")
    public Result updateCmsComponent(CmsComponentPo cmsComponentPo){
        try{
            cmsComponentService.update(cmsComponentPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("更新失败",e);
            return ResultUtils.error(500,"更新失败");
        }

    }

    /**
     * 删除CmsComponentPo
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsComponent/{id}")
    public Result deleteCmsComponent(@PathVariable("id") Integer id){
        CmsComponentPo cmsComponentPo=new CmsComponentPo();
        cmsComponentPo.setId(id);
        cmsComponentService.deleteCmsComponentPo(cmsComponentPo);
        return ResultUtils.success(ResultEnum.DELETED, null);
    }

    /**
     * 查看CmsComponentPo详情
     * @param id
     * @return result
     */
    @GetMapping("/cmsComponent/{id}")
    public Result getCmsComponent(@PathVariable("id") Integer id){
        CmsComponentPo cmsComponentPo=new CmsComponentPo();
        cmsComponentPo.setId(id);
        cmsComponentPo=cmsComponentService.get(cmsComponentPo);
        return ResultUtils.success(cmsComponentPo);
    }

    /**
     * 查询CmsComponentPo列表
     * @param cmsComponentPo
     * @return result
     */
    @GetMapping("/cmsComponents")
    public Result listAll(CmsComponentPo cmsComponentPo){
        List<CmsComponentPo> list=cmsComponentService.listAll(cmsComponentPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsComponentPo列表
     * @param cmsComponentPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsComponents/page")
    public Result listPage(CmsComponentPo cmsComponentPo,@RequestParam(value = "pageIndex",
        required = false, defaultValue = "1")
            Integer pageIndex,@RequestParam(value = "pageSize", required = false, defaultValue =
        "10") Integer pageSize){
        //根据组件名
        if (StringUtils.isNotEmpty(cmsComponentPo.getComponentName())) {
            cmsComponentPo.setComponentName("%".concat(cmsComponentPo.getComponentName())
                    .concat("%"));
        } else {
            cmsComponentPo.setComponentName(null);
        }
        QueryResult<CmsComponentPo> result=cmsComponentService.listPage(cmsComponentPo,pageIndex,pageSize);
        long total=result.getTotalrecord();
        Map<String, Object> map=new HashMap(16);
        if(total > 0){
            map.put("totalRecord",total);
            map.put("resultList",result.getResultlist());
        }
        return ResultUtils.success(map);
    }


    /**
     * 根据用户查询CmsComponentPo列表

     * @return result
     */
    @GetMapping("/cmsComponents/{userId}")
    public Result listByUser(@PathVariable("userId")Integer userId){
        CmsComponentPo cmsComponentPo=new CmsComponentPo();
        cmsComponentPo.setUserId(userId);
        List<CmsComponentPo> list=cmsComponentService.listByUser(cmsComponentPo);
        return ResultUtils.success(list);
    }
}

